//
//  ServiceListDelegate.m
//  Truffle
//
//  Created by m2sar on 11/03/10.
//  Copyright 2010 isrtream1-svn . All rights reserved.
//

#import "ServiceListRestDelegate.h"
#import "RestConnection.h"
#import "Service.h"
#import "ServiceListViewController.h"

@implementation ServiceListRestDelegate

@synthesize myPass, restConnection;

#pragma mark RestConnectionDelegate

- (void)willSendRequest:(NSURLRequest *)request
{
	NSLog(@">>[ServiceList -- will send request] %@",request);
}

- (void)didReceiveResponse:(NSURLResponse *)response
{
	NSLog(@">>[ServiceList -- didReceiveResponse] %@", response);
}

- (void)finishedReceivingData:(NSData *)data callback:(NSString *)restconn
{
	
	NSLog(@">>[ServiceList -- finishedReceivingData] %@", [restConnection stringData]);
	
	// Get the dictionary containing the raw parsed data.
	NSDictionary *rawServiceData = [restConnection dictionaryData];
	
	mutableServicesTable = [[NSMutableArray alloc] init];
	
	// Get the interesting search results data for display to the user.
	[servicesTableData release];
	servicesTableData = [[rawServiceData objectForKey:@"servicesdescriptors"] retain];
	
	for(int cpt=0;cpt<[servicesTableData count];cpt++) {
		Service *tempserv = [[Service alloc] initService:[[servicesTableData objectAtIndex:cpt] objectForKey:@"id"] withPosition:[[servicesTableData objectAtIndex:cpt] objectForKey:@"position"] withName:[[servicesTableData objectAtIndex:cpt] objectForKey:@"name"] withProvider:[[servicesTableData objectAtIndex:cpt] objectForKey:@"provider"]];
		[mutableServicesTable addObject:tempserv];
	}
	
	// STATICALLY ADD A SERVICE
	//[mutableServicesTable addObject:[[Service alloc] initService:@"STATICmyTestId" withPosition:@"STATICmyTestPosition" withName:@"STATICmyTestName" withProvider:@"STATICmyTestProvider"]];
	
	// return resulting table to passerelle
	[myPass receiveNewServiceTable:mutableServicesTable restURL:restconn];
	
}

@end
